import { Message } from "@arco-design/web-vue";
let messageHandler: any;
const TOAST_DURATION = 1500;
export const messageClose = () => {
	Message.clear();
	messageHandler?.close();
	messageHandler = null;
};

export class ToastUtils {
	static normal(content: string) {
		messageClose();
		messageHandler = Message.normal({
			duration: TOAST_DURATION,
			content
		});
	}

	static info(content: string) {
		messageClose();
		messageHandler = Message.info({
			duration: TOAST_DURATION,
			content
		});
	}

	static success(content: string) {
		messageClose();
		messageHandler = Message.success({
			duration: TOAST_DURATION,
			resetOnHover: true,
			content
		});
	}

	static warning(content: string) {
		messageClose();
		messageHandler = Message.warning({
			duration: TOAST_DURATION,
			resetOnHover: true,
			content
		});
	}

	static showError(message: string) {
		messageClose();
		messageHandler = Message.error({
			duration: TOAST_DURATION,
			resetOnHover: true,
			content: `${message}`
		});
	}

	static error = ToastUtils.showError;

	static loading(content: string) {
		messageClose();

		messageHandler = Message.loading({
			duration: TOAST_DURATION,
			resetOnHover: true,
			content
		});
		return messageHandler;
	}
}

export default ToastUtils;
